package com.example.service.impl;

import com.example.mapper.formListMapp;
import com.example.pojo.formListPojo;
import com.example.service.formListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName:formListImpl
 * Package:com.example.service.impl
 * Description:
 *
 * @Author：牧星苑
 * @Create 2025/9/19 12:41
 * @Version 1.0
 */
@Service
public class formListImpl implements formListService {
    @Autowired
    private formListMapp formListMapp;
    @Value("${file.upload-dir}")
    private String saveDir;

    @Override
    public String addFormSystemDocument(String id,
                                       String fileName,
                                       String number,
                                       String name,
                                       String nameEnglish,
                                       String nameRussian,
                                       String document_path,
                                       String upload_person) {
        // 创建文档对象
        formListPojo pojo = new formListPojo();

        // 设置基本信息
        pojo.setCreate_time(LocalDateTime.now());
        pojo.setUpdate_time(LocalDateTime.now());
        pojo.setSys_deleted(0);
        // 设置文件信息
        pojo.setId(id);
        pojo.setNumber(number);
        pojo.setName(name);
        pojo.setName_english(nameEnglish);
        pojo.setName_russian(nameRussian);
        pojo.setVersion_number("1.0");
        pojo.setDocument_path(document_path);
        pojo.setUpload_person(upload_person);
        pojo.setUpload_time(LocalDate.now().toString());
        try {
            // 保存到数据库
            formListMapp.addFormSystemDocument(pojo);
            return pojo.getDocument_path();
        } catch (Exception e) {
            System.err.println("保存PDF文档到数据库失败: " + e.getMessage());
            return "保存数据库失败: " + e.getMessage();
        }
    }
    public List<formListPojo> queryAll(){
        //调用formListMapp.queryAll()方法，将查询结果存储在pojoList变量中
        List<formListPojo> pojoList = formListMapp.queryAll();
        //直接返回从Mapper获取的结果列表
        return pojoList;
    }

    @Override
    public String updateFormDocument(formListPojo pojo) {
        try {
            System.out.println(pojo);
            pojo.setUpdate_time(LocalDateTime.now());
            pojo.setRevise_time(LocalDate.now().toString());
            formListMapp.updateFormDocument(pojo);
            return "文档信息更新成功";
        } catch (Exception e) {
            System.err.println("更新pdf文档失败: " + e.getMessage());
            return "更新文档失败: " + e.getMessage();
        }
    }

    @Override
    public formListPojo getDocumentById(String id) {
        return formListMapp.getDocumentById(id);
    }

    //下载表单
    public byte[] readFile(String fileName){
        File file = new File(saveDir + fileName);
        // 检查文件是否存在且可读
        if (file.exists() && file.canRead()) {
            try (FileInputStream fis = new FileInputStream(file)) {
                // 读取文件内容到byte[]数组中
                byte[] fileContent = fis.readAllBytes();
                return fileContent; // 返回文件内容的byte[]数组
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 如果文件不存在或不可读，或者读取时发生异常，返回null
        System.out.println("文件不存在或不可读：" + file.getAbsolutePath());
        return null;
    }
}
